package io.github.yotta.dict.dao.mybatisplus.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.github.yotta.common.api.model.Paging;
import io.github.yotta.dict.api.param.SysDictQueryParam;
import io.github.yotta.dict.dao.entity.SysDict;
import io.github.yotta.dict.dao.entity.SysDictDao;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

import java.util.stream.Collectors;


/**
 * @author yotta-generator
 * @date 2025-08-11 10:12:20
 * @description 系统字典表 - 数据访问层接口实现
 */
public class MybatisPlusSysDictDao implements SysDictDao {

    private final SysDictMybatisService sysDictMybatisService;

    public MybatisPlusSysDictDao(SysDictMybatisService sysDictMybatisService) {
        this.sysDictMybatisService = sysDictMybatisService;
    }

    @Override
    public List<SysDict> list(SysDictQueryParam queryParam) {
        return sysDictMybatisService.list(queryParam);
    }

    @Override
    public Paging<SysDict> page(SysDictQueryParam queryParam) {
        IPage<SysDict> page = sysDictMybatisService.page(queryParam);
        // Long total, List<T> data, Integer pageNo, Integer pageSize
        return new Paging<SysDict>(
                page.getTotal(),
                page.getRecords(),
                Math.toIntExact(page.getCurrent()),
                Math.toIntExact(page.getSize()));
    }

    @Override
    public SysDict getById(String tenantId, String id) {
        LambdaQueryWrapper<SysDict> queryWrapper = buildIdQuery(tenantId, id);
        return this.sysDictMybatisService.getOne(queryWrapper);
    }

    @Override
    public SysDict getByCode(String tenantId, String code) {
        LambdaQueryWrapper<SysDict> queryWrapper = buildCodeQuery(tenantId, code);
        return this.sysDictMybatisService.getOne(queryWrapper);
    }


    @Override
    public String add(SysDict sysDict) {
        this.sysDictMybatisService.save(sysDict);
        return sysDict.getId();
    }

    @Override
    public Boolean updateById(SysDict sysDict) {
        return this.sysDictMybatisService.updateById(sysDict);
    }

    @Override
    public Boolean deleteById(String tenantId, String id) {
        LambdaQueryWrapper<SysDict> queryWrapper = buildIdQuery(tenantId, id);
        return this.sysDictMybatisService.remove(queryWrapper);
    }


    @Override
    public List<String> listCategory(String tenantId) {
        LambdaQueryWrapper<SysDict> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotEmpty(tenantId), SysDict::getTenantId, tenantId);
        queryWrapper.select(SysDict::getCategory);
        List<SysDict> list = this.sysDictMybatisService.list(queryWrapper);
        return list.stream().map(SysDict::getCategory).collect(Collectors.toList());
    }


    private LambdaQueryWrapper<SysDict> buildIdQuery(String tenantId, String id) {
        LambdaQueryWrapper<SysDict> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(StringUtils.isNotEmpty(tenantId), SysDict::getTenantId, tenantId)
                .eq(SysDict::getId, id);
        return queryWrapper;
    }


    private LambdaQueryWrapper<SysDict> buildCodeQuery(String tenantId, String code) {
        LambdaQueryWrapper<SysDict> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(StringUtils.isNotEmpty(tenantId), SysDict::getTenantId, tenantId)
                .eq(SysDict::getCode, code);
        return queryWrapper;
    }

}
